<template>
<!-- 根节点标签 -->
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keywords" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getUsers">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="dataList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="权限" >
      </el-table-column>
      <el-table-column prop="url" label="地址" >
      </el-table-column>
      <el-table-column prop="descs" label="邮箱" >
      </el-table-column>
      <el-table-column prop="sn" label="编码描述" >
      </el-table-column>
      <el-table-column prop="parentId" label="父级id" >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>



    <!--分页条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="total, sizes, prev, pager, next, jumper" @current-change="handleCurrentChange" :page-size="pageSize" :page-sizes="[5, 10, 20, 50]" @size-change="handleSizeChange" :total="total" style="float:right;">
      </el-pagination>
    </el-col>


    <!--新增和编辑弹出框-->
    <el-dialog title="title" :visible.sync="FormVisible" :close-on-click-modal="false">
      <el-form :model="permissionForm" label-width="100px" :rules="permissionFormRules" ref="permissionForm">
        <el-form-item label="权限" prop="username">
          <el-input v-model="permissionForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="permissionForm.url" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-input v-model="permissionForm.descs" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="编码描述">
          <el-input v-model="permissionForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="父级id">
          <el-input v-model="permissionForm.parentId" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="FormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="permissionFormSubmit" :loading="permissionFormLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>

</template>

<script>
import util from "../../common/js/util";
import {editUser} from "../../api/api";

export default {
  name:"Permission",
  data(){
    return{
      query:{
        keywords:null
      },
      dataList:[],
      listLoading:false  ,
      sels:[],
      total:0,
      currentPage:1,
      pageSize:10,
      title:"新增",
      FormVisible:false,
      permissionForm:{
        id:null,
        name:null,
        url:null,
        descs:null,
        sn:null,
        parentId:null
      },
      permissionFormRules: {
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' }
        ]
      },
      permissionFormLoading:false,
      permissions:[],
      permissionTree:[]
    }
  },
  methods:{
    batchRemove(){
      //批量操作的触发
      var ids = this.sels.map(item => item.id)
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        //let para = { ids: ids };
        this.$http.patch("/permission",ids).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.loadListData();
        });
      }).catch(() => {

      });
    },
    //提交弹出框
    permissionFormSubmit(){
      this.$refs.permissionForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交数据吗？', '提示', {}).then(() => {
            this.permissionFormLoading = true;
            this.$http.put("/permission",this.permissionForm).then((res) => {
              if (res.data.success){
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
              }else {
                this.$message.error(res.data.message);
              }
              this.FormVisible = false;
              this.loadListData();
              this.permissionFormLoading = false;
            });
          });
        }
      });
    },
    getUsers(){
      this.currentPage =1;
      this.loadListData()
    },
    handleAdd(){
      //清空表单数据
      this.permissionForm ={
        id:null,
        name:null,
        url:null,
        descs:null,
        sn:null,
        parentId:null
      }
      this.FormVisible = true;
    },
    selsChange(sels){
      this.sels = sels;
    },
    //性别显示转换
    formatState: function (row, column) {
      return row.state == 1 ? '启用' : '废弃';
    },
    handleEdit(index,row){
      //数据回显
      Object.assign(this.permissionForm,row);
      //打开弹出框
      this.FormVisible = true;

    },
    handleDel(index,row){
      this.$confirm('确认删除数据吗？', '提示', { type: 'warning'}).then(() => {
        this.$http.delete("/permission/"+row.id).then((res) => {
          if (res.data.success){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            this.$message.error(res.data.message);
          }
          this.loadListData();
        });
      });
    },
    loadListData(){
      //加载列表数据
      this.listLoading = true;
      //封装请求数据
      let query={
        currentPage:this.currentPage,
        pageSize:this.pageSize,
        keywords:this.query.keywords
      }
      //发送请求列表获取数据
      this.$http.post("/permission",query).then(res=>{
        this.dataList=res.data.data.data;
        this.total = res.data.data.count;
      })
      //关闭加载效果
      this.listLoading =false;
    },
    handleCurrentChange(num){
      this.currentPage = num;
      this.loadListData();
    },
    //切换每页展示的条数时触发的方法
    handleSizeChange(num){
      this.pageSize = num;
      this.loadListData();
    },
    loadDepartment(){
      this.$http.get("/dep").then(res=>{
        this.pers = res.data.data
      })
    },
    //加载部门树
    loadDepTree(){
      this.$http.get("/permission/tree").then(res=>{
        this.depTree = res.data.data
      })
    }
  },
  mounted(){
    this.loadListData();
    this.loadDepartment();
  }
}
</script>

<style scoped>

</style>